const db_promise = require("../config/db.config.js");
const logger = require('../util/logger');

async function getAllEntities(req, res, next) {
  logger.winston.info('Entities.getAllEntities');
  
  try {
    	
    	 const [rows,fields] = await db_promise.query("select * from entities");
    	 res.status(200).json(rows);
    	
  }catch(err){

	    res.status(400).send({
	      message: err
	    });  

  }
    
  
}

async function getAllEntitiesForBot(req, res, next) {
  logger.winston.info('Entities.getAllEntitiesForBot');
  
  try {
    	
    	 const [rows,fields] = await db_promise.query("select * from entities where bot_id = ?  order by entity_id desc",[req.params.bot_id]);
    	 res.status(200).json(rows);
    	
  }catch(err){

	    res.status(400).send({
	      message: err
	    });  

  }
    
}

async function getSingleEntity(req, res, next) {
  logger.winston.info('Entities.getSingleEntity');
  
  try {
    	
    	 const [rows,fields] = await db_promise.query("select * from entities where entity_id = ?",[req.params.entity_id]);
    	 
    	 if (rows.length >= 1){
    	 	   rows_one = rows[0];
    	 }
    	 
    	 res.status(200).json(rows_one);   
    	     	 

    	
  }catch(err){

	    res.status(400).send({
	      message: err
	    });  

  }


}

async function createEntity(req, res, next) {
  logger.winston.info('Entities.createEntity');
  
  
    try {
    	
			  const [rows,fields] = await db_promise.query("insert into entities(bot_id, entity_name, slot_data_type) values (?,?,?)", [req.body.bot_id, req.body.entity_name, req.body.slot_data_type]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }
	  
	    

}

async function updateEntity(req, res, next) {
  logger.winston.info('entities.updateEntity');

    try {
    	
			  const [rows,fields] = await db_promise.query("update entities set entity_name = ?, slot_data_type = ? where entity_id = ?", [req.body.entity_name, req.body.slot_data_type, req.params.entity_id]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }


}

async function removeEntity(req, res, next) {
  logger.winston.info('entities.updateEntity');
  
    try {
    	
			  const [rows,fields] = await db_promise.query("delete from entities where entity_id = ?", [req.params.entity_id]);

        console.log(rows);			  
        if (rows.affectedRows >= 1){ 
			      res.status(200).json({ status: 'success', message: 'Updated' });
			  }else{
			  	  res.status(200).json({ status: 'no data', message: 'Updated' });
			  }    


  
    }catch(err){

		    res.status(400).send({
		      message: err
		    });  

	  }
    
  

}

module.exports = {
  getAllEntities,
  getAllEntitiesForBot,
  getSingleEntity,
  createEntity,
  updateEntity,
  removeEntity
};
